package com.common.domain;

import java.io.Serializable;

/**
 * 分页信息
 *
 * @author baixiaoshen
 */
public class Pageable implements Serializable {

    private static final long serialVersionUID = -3930180379790344299L;

    /**
     * 默认页码
     */
    private static final int DEFAULT_PAGE_NUMBER = 1;

    /**
     * 默认每页记录数
     */
    private static final int DEFAULT_PAGE_SIZE = 20;

    /**
     * 最大每页记录数
     */
    private static final int MAX_PAGE_SIZE = 1000;

    /**
     * 每页记录数
     */
    private int pageSize = DEFAULT_PAGE_SIZE;

    /**
     * 页码
     */
    private int pageNumber = DEFAULT_PAGE_NUMBER;

    /**
     * 分页起始行（0为第一行）
     */
    private int pageStart = 0;

    /**
     * 排序属性
     */
    private String orderProperty;

    /**
     * 筛选
     */
    private Filter filter = new Filter();

    /**
     * 初始化一个新创建的Pageable对象
     */
    public Pageable() {
    }

    /**
     * 初始化一个新创建的Pageable对象
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     */
    public Pageable(Integer pageNumber, Integer pageSize) {
        if (pageNumber != null && pageNumber >= 1) {
            this.pageNumber = pageNumber;
        }
        if (pageSize != null && pageSize >= 1 && pageSize <= MAX_PAGE_SIZE) {
            this.pageSize = pageSize;
        }
    }

    /**
     * 获取页码
     *
     * @return 页码
     */
    public int getPageNumber() {
        return pageNumber;
    }

    /**
     * 设置每页记录数
     *
     * @param pageSize 每页记录数
     */
    public void setPageSize(int pageSize) {
        if (pageSize < 1 || pageSize > MAX_PAGE_SIZE) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        this.pageSize = pageSize;

        setPageNumber(pageNumber);
    }

    /**
     * 设置页码
     *
     * @param pageNumber 页码
     */
    public void setPageNumber(int pageNumber) {
        if (pageNumber < 1) {
            pageNumber = DEFAULT_PAGE_NUMBER;
        }
        this.pageNumber = pageNumber;
        this.pageStart = (pageNumber - 1) * pageSize;
    }

    /**
     * 获取每页记录数
     *
     * @return 每页记录数
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 获取分页起始行
     *
     * @return 分页起始行
     */
    public int getPageStart() {
        return pageStart;
    }

    /**
     * 设置分页起始行
     *
     * @param pageStart 分页起始行
     */
    public void setPageStart(int pageStart) {
        this.pageStart = pageStart;
    }

    /**
     * 获取筛选
     *
     * @return 筛选
     */
    public Filter getFilter() {
        return this.filter;
    }

    /**
     * 设置筛选
     *
     * @param filter 筛选
     */
    public void setFilter(Filter filter) {
        this.filter = filter;
    }

    @Override
    public String toString() {
        return "Pageable{" +
                "pageNumber=" + pageNumber +
                ", pageSize=" + pageSize +
                ", pageStart=" + pageStart +
                ", orderProperty='" + orderProperty + '\'' +
                ", filter=" + filter +
                '}';
    }
}